//
//  Cell.m
//  TestMenu
//
//  Created by apple on 13-10-10.
//  Copyright (c) 2013年 apple. All rights reserved.
//

#import "Cell.h"

@implementation Cell
@synthesize labName;
@synthesize backImageView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.backImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        [self.backImageView setUserInteractionEnabled:YES];
        [self.contentView addSubview:self.backImageView];
        self.labName=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        [self.contentView addSubview:self.labName];
        UILongPressGestureRecognizer *longPregress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
        [self addGestureRecognizer:longPregress];
        [longPregress setMinimumPressDuration:0.4];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (BOOL)becomeFirstResponder
{
    return [super becomeFirstResponder];
   
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if(action == @selector(copy:))
        return YES;
    
    return [super canPerformAction:action withSender:sender];
}

- (void)copy:(id)sender
{
    [[UIPasteboard generalPasteboard] setString:@"11"];
    [self resignFirstResponder];
}



#pragma mark - Gestures
- (void)handleLongPress:(UILongPressGestureRecognizer *)longPress
{
    
    if(longPress.state != UIGestureRecognizerStateBegan|| ![self becomeFirstResponder])
    {
      //  NSLog(@"34343");
    
        //return;
        
    }
    NSLog(@"5555");
    
    
        UIMenuController *menu = [UIMenuController sharedMenuController];
    CGRect targetRect = [self convertRect:CGRectMake(0, 0, 320, 44)
                                 fromView:self];
    [menu setTargetRect:CGRectInset(targetRect, 0.0f, 4.0f) inView:self];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleMenuWillShowNotification:)
                                                 name:UIMenuControllerWillShowMenuNotification
                                               object:nil];
    [menu setMenuVisible:YES animated:YES];
    
    
    
}

#pragma mark - Notification
- (void)handleMenuWillHideNotification:(NSNotification *)notification
{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIMenuControllerWillHideMenuNotification
                                                  object:nil];
  //  self.bubbleView.selectedToShowCopyMenu = NO;
}

- (void)handleMenuWillShowNotification:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIMenuControllerWillShowMenuNotification
                                                  object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleMenuWillHideNotification:)
                                                 name:UIMenuControllerWillHideMenuNotification
                                               object:nil];

}


@end
